from django.conf import settings
from django.core.files.storage import Storage
from fdfs_client.client import Fdfs_client


class FDFSStorage(Storage):
    def _save(self, name, content):
        client = Fdfs_client()
        res = client.upload_by_buffer(content.read())
        if res.get('Status') != 'Upload successed.':
            raise Exception('上传文件到FDFS系统失败')

        # 获取返回的文件id
        file_id = res.get('Remote file_id')

        return file_id

    def exists(self, name):
        return False

    def url(self, name):
        return settings.SERVER_IP + name